import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kepu/page/game_page/provider/game_provider.dart';
import 'package:kepu/page/game_page/widgets/image_widget.dart';
import 'package:kepu/widgets/error_widget.dart';
import 'package:kepu/widgets/loading_widget.dart';
import 'package:kepu/widgets/textWithIcon.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'widgets/guess_button_widget.dart';
import 'widgets/refresh_button_widget.dart';

class GamePage extends ConsumerWidget {
  const GamePage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final size = MediaQuery.of(context).size;
    var game = ref.watch(gameProvider);
    var isAnimalGame = ref.watch(isAnimalGameTypeProvider);
    return Scaffold(
      appBar: AppBar(title: Text("猜一猜")),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
            padding: EdgeInsets.all(10),
            child: game.when(
              data: (data) {
                return Column(
                  children: [
                    //图片展览区
                    gameImage(size: size, imageUrl: data.image),
                    SizedBox(height: 50),

                    //名称
                    labelText(data.name, fontSize: 30),
                    SizedBox(height: 10),

                    //规则说明
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        GestureDetector(
                          onTap: () {
                            showGeneralDialog(
                              context: context,
                              pageBuilder: (
                                BuildContext buildContext,
                                Animation<double> animation,
                                Animation<double> secondaryAnimation,
                              ) {
                                return TDConfirmDialog(
                                  title: "游戏规则",
                                  content: "请根据图片判断其与名称是否匹配，若匹配则点击正确，否则错误",
                                );
                              },
                            );
                          },
                          child: textWithIcon(
                            text: "规则",
                            icon: TDIcons.questionnaire_double_filled,
                            color: Theme.of(context).primaryColor,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 40),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        guessButton(
                          size: size,
                          text: "错误",
                          isCorrect: !data.correct,
                          context: context,
                          buttonBgColor: Color(
                            0xffea4235,
                          ).withValues(alpha: 0.8),
                        ),
                        guessButton(
                          size: size,
                          text: "正确",
                          isCorrect: data.correct,
                          context: context,
                          buttonBgColor: Color(
                            0xff68c46c,
                          ).withValues(alpha: 0.8),
                        ),
                      ],
                    ),
                    SizedBox(height: 40),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 20),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          refreshButton(
                            text: isAnimalGame ? "去猜植物" : "去猜动物",
                            func: () {
                              ref.read(isAnimalGameTypeProvider.notifier).toggle();
                            },
                          ),
                          refreshButton(
                            text: "再来一次",
                            func: () {
                              ref.refresh(gameProvider.future);
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                );
              },
              error: (e, _) => errorWidget(),
              loading: () => loadWidget(context),
            ),
          ),
        ),
      ),
    );
  }
}